package com.nulldev.util.scripting.ScriptAPI.baseJS;

import com.nulldev.util.JVM.Arguments;
import com.nulldev.util.concurrency.threadIt.v4.emapi.uft.Executable;
import com.nulldev.util.manage.Unimplemented;
import com.nulldev.util.manage.Version;
import com.nulldev.util.scripting.ScriptAPI.baseJS.legacy.lexer.BaseJSEnvironment;
import com.nulldev.util.scripting.ScriptAPI.baseJS.legacy.lexer.BaseJSInterp;

/**
 * baseJS, a very minimalistic JavaScript engine, so simple that you SHOULDN'T
 * be using it. <br>
 * 
 * @author null
 * @since 3.0.0
 */
@Unimplemented
public class BaseJS {
	public static final float VERSION = 0.011f;
	public static final Version BASEJS_VERSION = new Version("0.0.11");

	public static final String[] SUPPORTED_FEATURES = new String[]
		{
				/* Arrays */
				"RETURN_ARRAY_READ", "ARRAY_READ_SET_VALUE",
				/* Math */
				"MATH_BASIC_NO_COMPLY", "MATH_FULLY_COMPLIANT", "MATH_RANDOM_VALS", "MATH_CONSTANTS",
				/* Values */
				"VALUES_FLOATING_POINT", "VALUES_INTEGER", "VALUES_STRING", "VALUES_ARRAY", "VALUES_BOOLEANS", "VALUES_ENCLOSED", "VALUES_UNDEFINED",
				/* Parsing */
				"PARSING_BASIC", "WHITESPACE_FILTER", "PARSING_ALLOCATION_PASS", "PARSING_ALLOW_BASIC_SPACING", "PARSING_EXECUTION",
				/* Execution */
				"RUNTIME_CLEANUP", "RUNTIME_CONSOLE_OUTPUT", "RUNTIME_DURING_PARSE", "RUNTIME_EVAL", "RUNTIME_GC" };

	public BaseJS() {
	}

	public BaseJS(final boolean useNewInterpreter) {
		this.useNewInterpreter = useNewInterpreter;
	}

	public volatile boolean useNewInterpreter = Arguments.hasArgument("--BaseJS.useNewInterpreter");

	public BaseJSEnvironment compile(final String code) {
		if (code == null || code.isEmpty())
			throw new NullPointerException("code == null!");
		return BaseJSInterp.get().interpret(this.useNewInterpreter, code);
	}

	public BaseJSEnvironment compile(final String code, final BaseJSEnvironment env) {
		if (code == null || code.isEmpty())
			throw new NullPointerException("code == null!");
		return BaseJSInterp.get().interpret(this.useNewInterpreter, code, env);
	}

	public Executable<BaseJSEnvironment> compileAsTask(final String code) {
		if (code == null || code.isEmpty())
			throw new NullPointerException("code == null!");
		return BaseJSInterp.get().interpretAsTask(this.useNewInterpreter, code);
	}
}
